<?php
require("config.php");
function getUser($user) {
	global $TBL_USER;
	$conn = connectDB();
	$sql = "select * from $TBL_USER where uid = '$user'";
	$result = mysql_query($sql, $conn);
	if(!$result) {
		echo mysql_error();
		return "ERR_USER_NONEXIST";
	}
	$row=mysql_fetch_row($result);
	$users['uid']   = $row[0];
	$users['cid']   = $row[1];
	$users['cname'] = $row[2];
	$users['rpass'] = $row[3];
	$users['email'] = $row[4];

	
	return $users;
	#$users=json_decode(file_get_contents("users/$user/settings.json"), true);
	#return $users;
}
function setUser($user, $data) {
	global $TBL_USER;
	$conn = connectDB();
	$sql = "select * from $TBL_USER where uid = '$user'";
	$result = mysql_query($sql, $conn);
	$uid = $user;
	$cid = $data['cid'];
	$cname = $data['cname'];
	$rpass = $data['rpass'];
	$email = $data['email'];

	if(!$result) {
		echo mysql_error();
		return "ERR_ERR";
	}
	if(mysql_num_rows($result) == 0) { //add new
		$sql = "insert into $TBL_USER (uid,cid,cname,rpass,email) values ".
				"('$uid','$cid','$cname','$rpass','$email')";
	}
	else
		$sql = "update $TBL_USER set uid='$uid',cid='$cid',cname='$cname',rpass='$rpass'".
				",email='$email' where uid='$uid'";

	$result = mysql_query($sql,$conn);
	if($result)
		return "OK_OK";
	else {
		echo mysql_error();
		return "ERR_ERR";
	}
	
	#file_put_contents("users/$user/settings.json",json_encode($data));
}
function saveOffer($uid, $content) {
	file_put_contents("users/$uid/offer.txt", $content);
}
function getOffer($user) {
	if(file_exists("users/$user/offer.txt"))
		return file_get_contents("users/$user/offer.txt");
	else
		return "";
		
}
function verifyUser($user, $passwd) {
	global $TBL_PASSDB;
	$conn = connectDB();
	$sql = "select * from $TBL_PASSDB where uid = '$user'";
	$result = mysql_query($sql, $conn);
	if(!$result) {
		echo mysql_error();
		return "ERR_USER_NONEXIST";
	}

	while($row=mysql_fetch_row($result)){
		$passdb[$row[0]] = $row[1];
		
	}

	#$passdb = json_decode(file_get_contents('passwd.json'),true);
    if(!isset($passdb[$user])) 
		return "ERR_USER_NONEXIST";
	else if($passdb[$user] != $passwd) 
		return "ERR_USER_PASSWD_MISMATCH";
	else if($passdb[$user] == $passwd)
		return "OK_OK";
	else
		return "ERR_UNKNOWN_CONDITION";
	
}
function modUser($user, $passwd) {
	global $TBL_PASSDB;
	$conn = connectDB();
	$sql = "update $TBL_PASSDB set passwd='$passwd' where uid='$user'";

	$result = mysql_query($sql, $conn);
	if(!$result)  {
		echo mysql_error();
		return "ERR_ERR;";
	}
	return "ERR_OK";
		#$passdb = json_decode(file_get_contents('passwd.json'),true);
		#$passdb[$user] = $passwd;
		#file_put_contents("passwd.json",json_encode($passdb));
}
function addUser($user, $passwd) {
	global $TBL_PASSDB;
	$conn = connectDB();
	$sql = "insert into $TBL_PASSDB (uid,passwd) values ('$user','$passwd')";

	$result = mysql_query($sql, $conn);
	if(!$result)  {
		echo mysql_error();
		return "ERR_ERR;";
	}
	mkdir("users/".$user);
	return "OK_OK";

	#$passdb = json_decode(file_get_contents('passwd.json'),true);
	#$passdb[$user] = $passwd;
	#file_put_contents("passwd.json",json_encode($passdb));
	#mkdir("users/".$user);
	#touch("users/".$user."/settings.json");
}

function compCntIncrease() {
	global $TBL_COUNTER;
	$conn = connectDB();
	$sql = "select * from $TBL_COUNTER where name='company'";

	$result = mysql_query($sql, $conn);
	if(!$result)  {
		echo mysql_error();
		return "ERR_ERR;";
	}
	$row=mysql_fetch_row($result);
	$row[1]++;

	$sql = "update $TBL_COUNTER set count=$row[1] where name='company'";
	$result = mysql_query($sql, $conn);
	if(!$result)  {
		echo mysql_error();
		return "ERR_ERR;";
	}
		
	return $row[1];
	#$COMPID = file_get_contents("compCnt");
	#$COMPID=(int)$COMPID + 1;
	##echo "COMPID $COMPID";
	#file_put_contents("compCnt",$COMPID);
	#return $COMPID;
}
function getCompUserMapping($cid) {
	global $TBL_CUMAP;
	$conn = connectDB();
	$sql = "select * from $TBL_CUMAP where cid=$cid";

	$result = mysql_query($sql, $conn);
	if(!$result)  {
		echo mysql_error();
		return "ERR_ERR;";
	}
	if(mysql_num_rows($result) == 0) 
		return "";
	
	$row=mysql_fetch_row($result);
	return $row[1];

	#$mappings = json_decode(file_get_contents("user_company_mapping.json"),true);
	#if(isset($mappings[$cid]))
	#	return $mappings[$cid];
	#else
	#	return "";	
}
function setCompUserMapping($cid, $user) {
	global $TBL_CUMAP;
	$conn = connectDB();
	$sql = "insert into $TBL_CUMAP (cid,uid) values ('$cid', '$user')";

	$result = mysql_query($sql, $conn);
	if(!$result)  {
		echo mysql_error();
		return "ERR_ERR;";
	}
	return "OK_OK";
	
	#$file = "user_company_mapping.json";
	#$mappings = json_decode(file_get_contents($file),true);
	#$mappings[$cid] = $user;
	#file_put_contents("$file",json_encode($mappings));
}
?>
